#include "widget.h"
#include "ui_widget.h"
#include"QRegExp"
#include"QString"
#include"QDebug"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setLayout(ui->gridLayout);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::setData()
{
    this->n = ui->lineEdit_n->text().toInt();

    QString str=ui->textEdit_shuzu->toPlainText();
    QRegExp innerPairPattern("\\[(\\d+),(\\d+)\\]");
    int pos = 0;

    while ((pos = innerPairPattern.indexIn(str, pos)) != -1) {
        // 提取匹配到的两个数字
        bool ok1, ok2;
        int num1 = innerPairPattern.cap(1).toInt(&ok1);
        int num2 = innerPairPattern.cap(2).toInt(&ok2);

        if (ok1 && ok2) {
            QVector<int> intPair = {num1, num2};
            this->Data.append(intPair);
        }
        // 跳过已经匹配的部分
        pos += innerPairPattern.matchedLength();
    }
    qDebug()<<this->Data<<endl;
}

QString Widget::to_Sting(bool flag)
{
    return flag ? "true" : "flase";
}


void Widget::on_pushButton_qingkong_clicked()
{
    this->Data.clear();
    ui->lineEdit_n->clear();
    ui->textEdit_shuzu->clear();
    ui->lineEdit_jieguo->clear();
}




void Widget::on_pushButton_ranse_clicked()
{
    this->setData();
    bool ret = Staining_method().possibleBipartition(this->n, this->Data);
    ui->lineEdit_jieguo->setText(to_Sting(ret));
    this->Data.clear();
}

void Widget::on_pushButton_bingchaji_clicked()
{
    this->setData();
    bool ret = union_find().possibleBipartition(this->n, this->Data);
    ui->lineEdit_jieguo->setText(to_Sting(ret));
    this->Data.clear();
}

void Widget::on_pushButton_tuichu_clicked()
{
    this->close();
}
